<template>
    <div class="export-btn-box">
        <el-button class="primary-btn" type="primary" @click="switchStatus(1)" :disabled="isExporting">{{isExporting ? '导出中...' : name}}</el-button>

        <el-dialog
            title="请选择签名导出的形式"
            :visible.sync="canShowStatus"
            width="35%"
            custom-class="export-dialog"
            :modal-append-to-body="false"
            :show-close="false">
            <div class="wrapper">
                <el-radio v-model="currStatus" :label="0" size="medium">路径形式</el-radio>
                <p class="remark-tips">备注：速度快，图片为路径显示。</p>
                <el-radio v-model="currStatus" :label="1" size="medium">图片形式</el-radio>
                <p class="remark-tips">备注：速度慢，图片为插入状态。</p>
            </div>
            <div slot="footer">
                <button class="el-button cancel-btn" @click="switchStatus(0)">取 消</button>
                <el-button type="primary" class="ex-btn" @click="switchStatus(2)">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { wasteExportUrl, wasteExportDltUrl, wasteExportDlUrl } from '../../common/ajax'
export default {
    props:{
        isDetailModal: {
          type: Boolean,
          default: false
        },
        name: {
            type: String,
            default: '导出结果'
        },
        currOrders: {
            type: Array
        },
        fileName: {
            type: String,
            default: ''
        },
        checkType: {
            type: Array,
            default: () => []
        }
    },
    data(){
        return{
            excelKey: '',
            loadingNotice: null,
            successNotice: null,
            isExporting: false,
            canShowStatus: false,
            currStatus: Cookies.get('categoryid') == '21'? 1: 0
        }
    },
    methods:{
        switchStatus(num){
            if(num == 0){
                this.canShowStatus = false;
            }else if(num == 1){
                if(this.$root.$data.is_szy){
                    this.exportExcel()
                    return
                }
                this.canShowStatus = true;
            }else if(num == 2){
                this.exportExcel()
            }
        },
        exportExcel(){
            let tips = '正在加载中，请稍候';

            this.loadingNotice = this.$notify({
                title: '提示',
                dangerouslyUseHTMLString: true,
                message: '<span>' + tips + '</span>',
                duration: 0,
                showClose: false
            })

            let key = ''
            let form = {
                arr: this.currOrders,
                code: 2,
                cs: this.checkType,
                status: this.currStatus,
                no_handover_time: this.isDetailModal ? 1 : 0  // 后端判断南北院时，去掉交接时间
            }

            this.isExporting = true;
            this.$emit('handleCancel', this.isExporting)

            jQuery.ajax({
                url: wasteExportUrl,
                type: 'post',
                data: JSON.stringify(form),
                success: function(data){
                    if(data.success){
                        this.excelKey = data.data;
                        this.loadingExcel()
                    }
                }.bind(this),
                fail: function(err){
                    alert(err)
                }
            })
        },
        loadingExcel(){
            let excelData = '';

            jQuery.ajax({
                url: wasteExportDltUrl,
                type: 'GET',
                data: { key: this.excelKey },
                success: function(data){
                    if(data.success){
                        if(data.data == 1) {
                            excelData = data.data;
                            this.isExporting = false;
                            this.$emit('handleCancel', this.isExporting)
                            this.showDownloadBtn()
                            clearInterval(timer)
                        }else{
                            var timer = setInterval(function() {
                                if(excelData == ''){
                                    jQuery.ajax({
                                        url: wasteExportDltUrl,
                                        type: 'GET',
                                        data: { key: this.excelKey },
                                        success: function(data){
                                            if(data.success){
                                                if(data.data == 1){
                                                    excelData = data.data
                                                    this.isExporting = false;
                                                    this.$emit('handleCancel', this.isExporting)
                                                    this.showDownloadBtn()
                                                    clearInterval(timer)
                                                }
                                            }
                                        }.bind(this)
                                    })
                                }
                            }.bind(this), 2000);
                        }
                    }
                }.bind(this)
            })
        },
        showDownloadBtn(){
            this.loadingNotice.close()

            const h = this.$createElement
            this.successNotice = this.$notify({
                title: '提示',
                dangerouslyUseHTMLString: true,
                message: h('div', null,
                [
                    h('span', null, '已完成加载，请点击按钮下载'),
                    h(
                        'button',
                        {
                            'class': {
                                'down-btn': true
                            },
                            style: {
                                border: '1px solid #409EFF',
                                backgroundColor: '#409EFF',
                                cursor: 'pointer',
                                padding: '5px 15px',
                                color: '#fff',
                                borderRadius: '5px',
                                fontSize: '12px',
                                marginLeft: '20px'
                            },
                            on:{
                                click: this.downloadExcel
                            }
                        },
                        '下载'
                    )
                ]),
                duration: 0,
            })
        },
        downloadExcel(){
            let fileName = this.fileName
            let xhr = new XMLHttpRequest()
            xhr.open('get', `${wasteExportDlUrl}?key=${this.excelKey}`, true);
            xhr.responseType = 'blob';
            xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
            xhr.onload = function () {
                if (this.status == 200) {
                    let blob = this.response;
                    let a = document.createElement('a');
                    let url = window.URL.createObjectURL(blob);
                    a.href = url;
                    a.download = `${fileName}.xlsx`;
                    a.click();
                }
            }
            xhr.send();
            this.successNotice.close();
        },
    }
}
</script>

<style>
.export-btn-box{
    display: inline-block;
}
</style>
